﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.Domain;
using MShop.Core.DataAccess;
 

namespace MShop.Web.Areas.Admin.Controllers
{
    public class BrandController : SecureController
    {
        private readonly IItemCatDao _ItemCatDao;
        private readonly IBrandDao _BrandDao;
        public BrandController(IItemCatDao itemcatdao,IBrandDao brandDao)
        {
            _BrandDao = brandDao;
            _ItemCatDao = itemcatdao;
        }
        
        public ActionResult Index()
        {
            
            return View(_BrandDao.GetAll());
        }
        public ActionResult Create()
        {
           
            return View(new Brand());
        }

        [HttpPost]
        public ActionResult Create(Brand brand)
        {
            
            if (ModelState.IsValid)
            {
                _BrandDao.Save(brand);
                _BrandDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(brand);
        }

        public ActionResult Edit(int id)
        {

            return View(_BrandDao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var brand = _BrandDao.GetById(id);
            _BrandDao.Delete(brand);
            _BrandDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, Brand brand)
        { 
            if (ModelState.IsValid)
            {
                var dbbrand = _BrandDao.GetById(id);
                UpdateModel<Brand>(dbbrand, new string[] { "Name", "Icon", "Desc" });
                _BrandDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(brand);

            
        }
    }
}
